package fiuba.webapp.action;

import com.opensymphony.xwork2.Preparable;
import fiuba.service.GenericManager;
import fiuba.service.MenuManager;
import fiuba.model.Estructura;
import fiuba.model.ListaNoticias;
import fiuba.model.Menu;
import fiuba.model.Seccion;
import fiuba.webapp.action.BaseAction;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SeccionAction extends BaseAction implements Preparable {
    private GenericManager<Seccion, Long> seccionManager;
    private GenericManager<Estructura, Long> estructuraManager;
    private GenericManager<ListaNoticias, Long> listaNoticiasManager;
    private MenuManager menuManager;
    private List seccions;
    private Seccion seccion;
    private Long  id;
    private String estructuraId;
    private String menuId;
    private String listaId;
    
    
    public String getListaId() {
		return listaId;
	}

	public void setListaId(String listaId) {
		this.listaId = listaId;
	}

	public void setListaNoticiasManager(
			GenericManager<ListaNoticias, Long> listaNoticiasManager) {
		this.listaNoticiasManager = listaNoticiasManager;
	}

	public void setSeccionManager(GenericManager<Seccion, Long> seccionManager) {
        this.seccionManager = seccionManager;        
    }

    public List getSeccions() {
        return seccions;
    }
    
    public Map<String,String> getEstructuras() {
		Map<String,String> ret = new LinkedHashMap<String,String>();
		 List <Estructura> estructuras = estructuraManager.getAll();		    
	     for (Estructura e : estructuras){
		   	ret.put(e.getId().toString(),e.getDescripcion());
	     }
		return ret;
    }

    public Map<String,String> getMenues() {
        Map<String,String> ret = new LinkedHashMap<String,String>();
		List <Menu> menus = menuManager.getMenus();
		 for (Menu m : menus){
		   	ret.put(m.getId().toString(),m.getName());
	     }	     
		return ret;
    }

    public Map<String,String> getListaDeNoticias() {
        Map<String,String> ret = new LinkedHashMap<String,String>();
		List <ListaNoticias> listaNoticias = listaNoticiasManager.getAll();
		 for (ListaNoticias l : listaNoticias){
		   	ret.put(l.getId().toString(),l.getNombre());
	     }	     
		return ret;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String seccionId = getRequest().getParameter("seccion.id");
            if (seccionId != null && !seccionId.equals("")) {
                seccion = seccionManager.get(new Long(seccionId));
            }
        }
    }

    public String list() {
        seccions = seccionManager.getAll();
        return SUCCESS;
    }

    public void setId(Long  id) {
        this. id =  id;
    }

    public Seccion getSeccion() {
        return seccion;
    }

    public void setSeccion(Seccion seccion) {
        this.seccion = seccion;
    }

    public String delete() {
        seccionManager.remove(seccion.getId());
        saveMessage(getText("seccion.deleted"));
        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            seccion = seccionManager.get(id);
            estructuraId = seccion.getEstructura().getId().toString();
            menuId = seccion.getMenu().getId().toString();
            listaId = seccion.getListaNoticias().getId().toString();
        } else {
            seccion = new Seccion();
            estructuraId = "";
            menuId = "";
            listaId = "";
        }
        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }
        seccion.getRoles().clear(); // APF-788: Removing roles from user doesn't work
     /*   String[] userRoles = getRequest().getParameterValues("seccionRoles");

        for (int i = 0; userRoles != null && i < userRoles.length; i++) {
            String roleName = userRoles[i];
            seccion.addRole(roleManager.getRole(roleName));
        }
*/
        
        Menu menu = menuManager.getMenu(menuId); 
        Estructura estructura = estructuraManager.get(Long.valueOf(estructuraId));
        ListaNoticias listaNoticia = listaNoticiasManager.get(Long.valueOf(listaId));
        boolean isNew = (seccion.getId() == null);
        seccion.setEstructura(estructura);
        seccion.setMenu(menu);
        seccion.setListaNoticias(listaNoticia);
        
        seccionManager.save(seccion);
        
        String key = (isNew) ? "seccion.added" : "seccion.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

	public String getEstructuraId() {
		return estructuraId;
	}

	public void setEstructuraId(String estructuraId) {
		this.estructuraId = estructuraId;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public void setEstructuraManager(
			GenericManager<Estructura, Long> estructuraManager) {
		this.estructuraManager = estructuraManager;
	}

	public void setMenuManager(MenuManager menuManager) {
		this.menuManager = menuManager;
	}
}